package com.inf.utils;

import ch.qos.logback.classic.gaffer.PropertyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by zhangbn on 2018/11/29.
 */
public class PropertiesUtil {

    public static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
    private static Properties props;

    static {
        loadProps();
    }

    synchronized static private void loadProps() {
        if (null == props) {
            logger.info("show: 开始加载properties文件内容.......");
            props = new Properties();
            InputStream in = null;
            try {
                in = PropertiesUtil.class.getClassLoader().getResourceAsStream("config/personface.properties");
                props.load(in);
            } catch (FileNotFoundException e) {
                logger.error("properties文件未找到");
            } catch (IOException e) {
                logger.error("出现IOException");
            } finally {
                try {
                    if (null != in) {
                        in.close();
                    }
                } catch (IOException e) {
                    logger.error("properties文件流关闭出现异常");
                }
            }
            logger.info("show: 加载properties文件内容完成");
            //logger.info("properties文件内容：" + props);
        }
    }

    public static String getProperty(String key) {
        if (null == props) {
            loadProps();
        }
        return props.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        if (null == props) {
            loadProps();
        }
        return props.getProperty(key, defaultValue);
    }
}
